package com.smec.moblie.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smec.moblie.R;
import com.smec.moblie.bean.MaintenancePhotoJobitemBean;
import com.smec.moblie.common.ConstValue;

import java.io.File;
import java.util.ArrayList;

/**
 * 照片项适配器
 * Created by sunxinwei on 16/2/2.
 */
public class MaintenancePhotoJobitemAdapter extends BaseAdapter implements ConstValue {
    private LayoutInflater inflater;
    private Context context;
    private ItemHolder itemHolder;
    private ArrayList<MaintenancePhotoJobitemBean> maintenancePhotoJobitemBean;
    private String imgPath;
    private DisplayImageOptions options;

    public MaintenancePhotoJobitemAdapter(Context context) {
        this.context = context;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imgPath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";

        options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.photo).showImageForEmptyUri(R.mipmap.photo).showImageOnFail(R.mipmap.photo)
                .cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
                .build();
    }

    public void setListData(ArrayList<MaintenancePhotoJobitemBean> maintenancePhotoJobitemBean) {
        this.maintenancePhotoJobitemBean = maintenancePhotoJobitemBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return maintenancePhotoJobitemBean != null ? maintenancePhotoJobitemBean.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return maintenancePhotoJobitemBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.item_maintenancephotojob, null);
        itemHolder = new ItemHolder();
        itemHolder.photojobTv = (TextView) convertView.findViewById(R.id.photojobTv);
        itemHolder.photojobImg = (ImageView) convertView.findViewById(R.id.photojobImg);
        ImageLoader imageLoader = ImageLoader.getInstance();

        if (!maintenancePhotoJobitemBean.get(position).getAction_type_result().equals("")) {
            itemHolder.photojobImg.setBackgroundColor(Color.parseColor("#00FFFFFF"));

            File file1 = new File(imgPath + maintenancePhotoJobitemBean.get(position).getAction_type_result() + ".jpeg");
            if (file1.exists()) {
                imageLoader.displayImage("file://" + imgPath + maintenancePhotoJobitemBean.get(position).getAction_type_result() + ".jpeg", itemHolder.photojobImg, options);
            } else {
                imageLoader.displayImage(SERVER_BASE_PATH + "rest/resources/" + maintenancePhotoJobitemBean.get(position).getAction_type_result(), itemHolder.photojobImg, options);
            }
        }
        itemHolder.photojobTv.setText(maintenancePhotoJobitemBean.get(position).getJob_no() + " " + maintenancePhotoJobitemBean.get(position).getJob_name());
        return convertView;
    }

    public class ItemHolder {
        public TextView photojobTv;
        public ImageView photojobImg;
    }
}
